package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Exceptions.CheckItemException;
import com.itheima.dao.PermissionMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @outhor
 * @date 2020/2/10 19:57
 */

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Permission> permissionList = permissionMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),permissionList);
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionMapper.findById(id);
    }

    @Override
    public void updata(Permission permission) {
        permissionMapper.updata(permission);
    }

    @Override
    public void delete(Integer id) {
        Integer count = permissionMapper.findRolAndPermissionById(id);
        if (count > 0) {
            throw new CheckItemException("存在引用,不能删除");
        } else {
            permissionMapper.delete(id);
        }
    }
}
